import Mock from "mockjs";

/**
 * 分页获取课程列表
 *
 * 过程：在product_spu表中获取全部spu_type=2的商品的id，再通过这些id去product_sku中spu_id=product_spu.id的sku，
 * 再通过sku_id去sku_bind表里面查sku_id=sku.id的bind_id，再通过bind_id去course表里面查course.id=bind_id的课程
 *
 * select * from train_product_spu where spu_type=2 limit ${params.pageSize} offset ${params.pageSize * (params.pageNo - 1)}
 *  left join train_product_sku on train_product_sku.spu_id=train_product_spu.id    -- 取得sku价格、图片
 *  left join train_product_sku_bind on train_product_sku_bind.sku_id=train_product_sku.id
 *  left join train_course on train_product_sku_bind.bind_type=1 and train_course.id=train_product_sku_bind.bind_id -- 取得课程信息
 *
 * select count(*) from train_member_course_mapping where member_id=${memberId} and course_id=${id} -- 判断是否已购买该课程
 *
 */
export const getCoursePage = async (params: PageParams & { category: number }): Promise<{ data: ListCourseWithPrice[]; total: number }> => {
  const res = await Mock.mock({
    "data|10": [
      {
        id: "@integer(1, 100)",
        courseName: "@ctitle",
        courseCover: "@image(336x230, @color, @word)",
        isRecommend: "@boolean",
        sortNo: "@integer(1, 100)",

        price: "@integer(1, 100)",

        purchased: "@boolean",
      },
    ],
  });

  return {
    data: res.data,
    total: 100,
  };
};

/**
 * 获取已购买的课程列表
 *
 * 过程：在member_course表中获取用户绑定的课程id，然后联表查询course表，获取所有的course信息
 *
 * select * from train_member_course_mapping where member_id=${memberId}
 *  left join train_course on train_course.id=train_member_course_mapping.course_id
 *
 */
export const getPurchasedCourseList = async (): Promise<ListCourse[]> => {
  const res = await Mock.mock({
    "data|10": [
      {
        id: "@integer(1, 100)",
        courseName: "@ctitle",
        courseCover: "@image(336x230, @color, @word)",
        isRecommend: "@boolean",
      },
    ],
  });

  return res.data;
};

/**
 * 根据课程id获取课程，根据课程id在 train_product_sku_bind 中查找 bind_type=1 并且 bind_id=course.id 的 sku_id
 * 再根据 sku_id 在 train_product_sku 中查找 sku_id=sku.id 的 sku，填入到 course 中
 *
 * select * from train_product_spu where spu_type=2 limit ${params.pageSize} offset ${params.pageSize * (params.pageNo - 1)}
 *  left join train_product_sku on train_product_sku.spu_id=train_product_spu.id     -- 取得sku价格、图片
 *  left join train_product_sku_bind on train_product_sku_bind.sku_id=train_product_sku.id
 *  left join train_course on train_product_sku_bind.bind_type=1 and train_course.id=train_product_sku_bind.bind_id -- 取得课程信息
 *
 * select count(*) from train_member_course_mapping where member_id=${memberId} and course_id=${id} -- 判断是否已购买该课程
 *
 * @param id - 课程id
 */
export const getCourseById = async (id: number): Promise<Course> => {
  const res = await Mock.mock({
    id,
    knowledgeFrameworkId: "@integer(1, 100)",
    courseName: "@ctitle",
    courseCover: "@image(336x230, @color, @word)",
    // 返回html文章
    courseDetail: `
      <p>@cparagraph(3, 5)</p>
      <img src="@image(336x230, @color, @word)" />
      <p>@cparagraph(3, 5)</p>
    `,
    courseItemType: "@integer(1, 2)",
    courseStatus: "@integer(1, 2)",
    isRecommend: "@boolean",
    validDays: "@integer(1, 100)",
    sortNo: "@integer(1, 100)",

    skuId: "@integer(1, 100)",
    price: "@integer(1, 100)",
    skuPic: "@image(336x230, @color, @word)",

    purchased: "@boolean",
  });

  return res;
};

/**
 * 获取直播/回放链接
 * @param id 课程直播记录id
 * @param status 1 上课中 0 不在上课中
 * 根据状态，构造不同的url返回
 * 上课中： 参考https://dev.baijiayun.com/wiki/detail/57#-h0文档
 * 未上课：根据课程直播记录id去课程直播表拿到房间号，合作方用户名，查询直播回放信息，返回回放记录url，若没有，返回空字符串
 * 回放参考文档：查询直播回放信息：https://dev.baijiayun.com/wiki/detail/6#-h2-3 拼接字符串：群里信息
 * * @returns
 */
export const getLiveUrl = async (params: { courseItemId: number; status: number }): Promise<string> => {
  const res = await Mock.mock({
    url: "https://www.baidu.com",
  });
  return res;
};

/**
 * 通过视频id，获取视频的token
 * @param vid 视频id
 * @returns
 */
export const getVideoToken = async (vid: string): Promise<Res<{ token: string }>> => {
  const res = await Mock.mock({
    data: {
      token: `123456789`,
    },
  });
  return res;
};

/**
 * 如果已经有记录，则更新，没有则插入
 * 更新课程学习进度
 *  */

export const updateStudyRecord = (params: { courseId: string; courseItemId: number; perentage: number }) => {
  return Mock.mock({
    isSuccedd: true,
  });
};

// -------------------------------------------------------------------
/**
 * 获取课程条目的详情
 * 以下接口返回的条目类型数据详情，在前端会与 通过条目接口获取到的条目列表 进行合并组合
 * 以下接口只需要去对应的类型表里返回额外需要的信息即可
 */
export const getCourseItemDetail = async (params: object) => {
  return Mock.mock({
    data: [
      {
        // 视频类型的条目
        courseItemId: "1691354063012237312",
        videoDuration: "@integer(1, 100)",
      },
      {
        // 视频类型的条目
        courseItemId: "1691354063012237313",
        videoDuration: "@integer(1, 100)",
      },
      {
        // 直播类型的条目
        courseItemId: "1691354063012237314",
        liveTime: "@datetime", // 2023-01-01 12:00 - 16:00
        liveStatus: "@integer(0, 1, -1)", // 0 看回放 1 直播中 2 未开播
      },
      {
        // 图片/题目类型的条目
        courseItemId: "1691354063012237315",
        studyProgress: "@integer(1, 100)", // 课程学习记录表查询
      },
    ],
  });
};

/** 传入视频id，获取答题节点及题目 */
export const getCourseVideoQuestion = async (id: string): Promise<Res<CourseVideoQuestion>> => {
  return Mock.mock({
    "data|4": [
      {
        time: "@integer(1, 60)",
        question: {
          id: "@integer(1, 100)",
          title: "@ctitle",
          options: {
            A: "@ctitle",
            B: "@ctitle",
            C: "@ctitle",
            D: "@ctitle",
          },
          answerIndex: ["A"],
          official: "@ctitle",
          quType: "@integer(1, 3)",
        },
      },
    ],
  });
};
